package cn.edu.sdjzu.xg.bysj.service;


import cn.edu.sdjzu.xg.bysj.dao.GraduateProjectTypeDao;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProject;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProjectType;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public final class GraduateProjectTypeService {
	private static GraduateProjectTypeService graduateProjectTypeService = new GraduateProjectTypeService();
	private GraduateProjectTypeDao graduateProjectTypeDao = GraduateProjectTypeDao.getInstance();
	
	private GraduateProjectTypeService(){}
	
	public static GraduateProjectTypeService getInstance(){
		return graduateProjectTypeService;
	}

	public Collection<GraduateProjectType> findAll()throws SQLException {
		Connection connection = JdbcHelper.getConn();
		Collection<GraduateProjectType> graduateProjectTypes=graduateProjectTypeDao.findAll(connection);
		JdbcHelper.close(connection);
		return graduateProjectTypes;
	}

	public GraduateProjectType find(Integer id)throws SQLException {
		Connection connection = JdbcHelper.getConn();
		GraduateProjectType graduateProjectType=graduateProjectTypeDao.find(id,connection);
		JdbcHelper.close(connection);
		return graduateProjectType;
	}
	
	public boolean update(GraduateProjectType graduateProjectType)throws SQLException {
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		Boolean updated =false;
		try {
			//增加用户
			updated = graduateProjectTypeDao.update(graduateProjectType,connection);
			//提交事务
			connection.commit();

		}catch (Exception e){
			//回滚
			connection.rollback();

		}finally {
			//将自动提交设为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(connection);
		}
		return updated;
	}
	
	public boolean add(GraduateProjectType graduateProjectType)throws SQLException {
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		Boolean added =false;
		try {
			//增加用户
			added = graduateProjectTypeDao.add(graduateProjectType,connection);
			//提交事务
			connection.commit();

		}catch (Exception e){
			//回滚
			connection.rollback();

		}finally {
			//将自动提交设为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(connection);
		}
		return added;
	}

	public boolean delete(Integer id)throws SQLException {
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		Boolean deleted =false;
		try {
			//增加用户
			deleted = graduateProjectTypeDao.delete(id,connection);
			//提交事务
			connection.commit();

		}catch (Exception e){
			//回滚
			connection.rollback();

		}finally {
			//将自动提交设为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(connection);
		}
		return deleted;
	}

}
